// pages/integralDetail/integralDetail.js
var http = require("../../../utils/http.js");
var util = require("../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pages: 0,
    current: 1,
    size: 10,
    scoreDetails: [],  //积分明细列表
    isAll: false,
    userInfo: {}, //用户积分等级信息
    scoreExplain: {},  //积分说明
    showPop: false,  //攻略弹框显隐
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //请求积分明细
    this.getScoreDetails()
    //获取用户积分等级信息
    this.getUserScore()
    //积分攻略
    this.getScoreExplain()
  },

  /**
   * 获取用户积分等级信息
   */
  getUserScore: function() {
    var param = {
      url: '/p/user/userInfo',
      method: 'GET',
      data: {
        current: this.data.current,
        size: 10
      },
      callBack: (res) => {
        this.setData({
          userInfo : res
        })
      }
    }
    http.request(param)
  },

  /**
   * 请求积分明细
   */
  getScoreDetails: function() {
    wx.showLoading()
    var param = {
      url: '/p/score/page',
      method: 'GET',
      data: {
        current: this.data.current,
        size: 10
      },
      callBack: (res) => {
        wx.hideLoading()
        var scoreDetails = []
        if (this.data.current == 1) {
          this.setData({
            scoreDetails: res.records,
            pages: res.pages,
            current: res.current
          });
        } else {
          scoreDetails = this.data.scoreDetails
          scoreDetails.push(...res.records)
          this.setData({
            scoreDetails
          })
        }
      }
    }
    http.request(param);
  },

  /** 
   * 触底加载下一页 
   */
  getNextPage() {
    if (this.data.pages > this.data.current) {
      this.setData({
        current: this.data.current + 1
      })
      this.getScoreDetails()
    } else {
      this.setData({
        isAll: true
      })
    }
  },

  /**
   * 积分攻略
   */
  getScoreExplain: function() {
    var param = {
      url: '/p/score/getScoreQuestion',
      method: 'GET',
      data: {},
      callBack: (res) => {
        res.paramValue = util.formatHtml(res.paramValue);
        this.setData({
          scoreExplain: res
        })
      }
    };
    http.request(param)
  },

  /**
   * 积分攻略弹框显隐
   */
  clikcPop: function() {
    var showPop = !this.data.showPop
    this.setData({
      showPop
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getNextPage()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})